package com.treasure.service.commons.lang.util;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * @ClassName : ParamUtils
 * @author : panxingwu
 * @date : 2018年5月30日 下午8:26:47
 * @Description : 参数校验工具类
 */
public class ParamUtils {
	private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

	/** 
	* @Title      :validate 
	* @param      :@param t
	* @param      :@return 
	* @return     :String 
	* @Description:参数校验
	*/
	public static <T> String validate(T t) {
		Validator validator = factory.getValidator();
		Set<ConstraintViolation<T>> constraintViolations = validator.validate(t);
		for (ConstraintViolation<T> constraintViolation : constraintViolations) {
			String message = constraintViolation.getMessage();
			if(StringUtil.isNotBlank(message)) return message;
		}
		return null;
	}
}
